package xj.toolkit.netty.transport;

import com.google.common.base.Preconditions;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import xj.toolkit.netty.transport.endpoint.Endpoint;

public class TransportUtils extends xj.toolkit.base.TransportUtils {

	private static final AttributeKey<Endpoint> TRANSPORT_ENDPOINT = AttributeKey.valueOf("TRANSPORT_ENDPOINT");

	public static void attachEndpointToSession(ChannelHandlerContext ctx, Endpoint endpoint) {
		Preconditions.checkArgument(ctx != null);
		ctx.channel().attr(TRANSPORT_ENDPOINT).set(endpoint);
	}

	public static Endpoint getEndpointOfSession(ChannelHandlerContext ctx) {
		Preconditions.checkArgument(ctx != null);
		return ctx.channel().attr(TRANSPORT_ENDPOINT).get();
	}

	public static void attachEndpointToSession(Channel channel, Endpoint endpoint) {
		Preconditions.checkArgument(channel != null);
		channel.attr(TRANSPORT_ENDPOINT).set(endpoint);
	}

	public static Endpoint getEndpointOfSession(Channel channel) {
		Preconditions.checkArgument(channel != null);
		return channel.attr(TRANSPORT_ENDPOINT).get();
	}

}
